
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 *
 * @author Jason
 */
public class Mailer {

    private static final String MAIL_TRANSPORT_PROTOCOL = "smtp";
    private static final String MAIL_SMTP_HOST = "mail.lotary.com.cn";
    private static final String MAIL_SMTP_PORT = "25";

    public static void sendMail(String from, String to, String subject, String body) {
        System.setProperty("mail.mime.charset", "UTF-8");

        // TODO code application logic here
        Properties properties = new Properties();
        properties.put("mail.transport.protocol", MAIL_TRANSPORT_PROTOCOL);
        properties.put("mail.smtp.host", MAIL_SMTP_HOST);
        properties.put("mail.smtp.port", MAIL_SMTP_PORT);
        Session session = Session.getDefaultInstance(properties, null);

        MimeMessage message = new MimeMessage(session);
        try {
            message.setFrom(new InternetAddress(from));
            message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
            message.setSubject(subject);
            message.setContent(body, "text/html;charset=utf-8");
            Transport.send(message);
        } catch (AddressException e) {
        } catch (MessagingException e) {
        }
    }
}
